/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == nullptr) {
            return false;
        }

        ListNode* pFast = head;
        ListNode* pSlow = head;
        while((pFast != nullptr) && (pFast->next != nullptr)) {
            pFast = pFast->next->next;
            pSlow = pSlow->next;

            if(pFast == pSlow) {
                return true;
            }
        }
        return false;
    }
};